package com.csu.backmanagesys.Controller;

import com.csu.backmanagesys.Domain.Admin;
import com.csu.backmanagesys.Service.AdminService;
import com.csu.backmanagesys.Service.SendSmsService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.Objects;

@Controller
public class SignOnController {
    @GetMapping("/signOn")
    public ModelAndView signOn(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("signon");
        return modelAndView;
    }

    @Autowired
    SendSmsService sendSmsService;
    @Autowired
    AdminService adminService;

    private String code;
    private String phone;

    @PostMapping("/send")
    public ModelAndView sendSms(HttpServletRequest req){
        System.out.println("__________");
        System.out.println(req.getParameter("phone"));

        HttpSession session = req.getSession();
        this.phone = req.getParameter("phone");
        session.setAttribute("tryPhone", phone);
        session.setAttribute("loginMethod", 2);

        Admin admin = adminService.selectAdminByPhone(phone);
        if(admin == null){
            session.setAttribute("signOnMsg" , "该手机号无管理员账号");
        }else {
            this.code = sendSmsService.getCode();
            System.out.println(this.code);
            //发送验证码
            try {
                sendSmsService.sendMessage(phone,code);
                session.setAttribute("signOnMsg" , "验证码已发送");
            } catch (Exception e) {
                e.printStackTrace();
                session.setAttribute("signOnMsg" , "验证码发送失败，请稍后再试");
            }
        }
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("signon");
        return modelAndView;
    }

    public String msg;
    @PostMapping("/index")
    public ModelAndView signOnCheck(Model model, HttpServletRequest req){
        System.out.println("++++++++++++++");

        String loginMethod = req.getParameter("loginMethod");
        System.out.println(loginMethod+"____________");

        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String phoneCheck = req.getParameter("phone");
        String codeCheck = req.getParameter("code");
        HttpSession session = req.getSession();
        session.setAttribute("tryUsername" , username);
        session.setAttribute("tryPassword", password);
        session.setAttribute("tryPhone", phoneCheck);
        session.setAttribute("loginMethod", loginMethod);

        ModelAndView modelAndView = new ModelAndView();
        System.out.println(username+password);
        if(!loginMethod.equals("2")){
            //检验用户或密码
            Admin admin = adminService.getAdminById(username);
            System.out.println(admin.getAdminPhone());
            if(admin == null){
                this.msg = "该账号不存在";
                session.setAttribute("signOnMsg", this.msg);
                modelAndView.setViewName("signon");
                return modelAndView;
            }else {
                String rightPassword = admin.getAdminPassword();
                System.out.println(admin+rightPassword);
                if ( !Objects.equals(DigestUtils.md5DigestAsHex(password.getBytes()), rightPassword)) {
                    this.msg = "用户名或密码错误";
                    session.setAttribute("signOnMsg", this.msg);

                    modelAndView.setViewName("signon");
                    return modelAndView;
                } else {
                    session.setAttribute("signOnMsg", "");
                    session.setAttribute("tryPassword", null);
                    session.setAttribute("admin", admin);

                    modelAndView.setViewName("index");
                    return modelAndView;
                }
            }
        }else {
            //检验手机号码和验证码
            System.out.println(this.phone);
            if(!Objects.equals(this.phone, phoneCheck)){
                this.msg = "该手机号未接收过验证码";
                session.setAttribute("signOnMsg", this.msg);

                modelAndView.setViewName("signon");
                return modelAndView;
            }else {
                Admin admin = adminService.selectAdminByPhone(phoneCheck);
                if (admin == null || !Objects.equals(codeCheck, code)) {
                    this.msg = "手机号或验证码不正确";
                    session.setAttribute("signOnMsg", this.msg);

                    modelAndView.setViewName("signon");
                    return modelAndView;
                } else {
                    session.setAttribute("signOnMsg", "");
                    session.setAttribute("admin", admin);

                    modelAndView.setViewName("index");
                    return modelAndView;
                }
            }
        }

    }

    @GetMapping("/signOut")
    public ModelAndView signOut(HttpServletRequest req){
        HttpSession session = req.getSession();
        session.setAttribute("admin" , null);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("signon");
        return modelAndView;
    }
}
